/*
 * Copyright (c) 2011, 2014, Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 *
 * This file is available and licensed under the following license:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  - Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the distribution.
 *  - Neither the name of Oracle nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.sjg.dom;

import com.alibaba.fastjson.JSONObject;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import redis.clients.jedis.Jedis;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class htmlread extends Application {

    private Scene scene;

    @Override
    public void start(Stage stage) {
        // create scene
        stage.setTitle("Web View Sample");
        scene = new Scene(new Browser(stage), 1000, 100, Color.web("#666970"));
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

class Browser extends Region {

    private final HBox toolBar;
    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();
    final Button toggleHelpTopics = new Button("Toggle Help Topics");
    final WebView smallView = new WebView();
    private boolean needDocumentationButton = false;
    private static String awardNumbers_c_st="1,3,5,7,9";
    
    
    public Browser(final Stage stage) {
        toolBar = new HBox();
        toolBar.setAlignment(Pos.CENTER);
        toolBar.getStyleClass().add("browser-toolbar");
        toolBar.getChildren().add(createSpacer());
        Button bt=new Button("开始");
        bt.setPrefWidth(1000);
        bt.setPrefHeight(100);
        bt.setOnAction((ActionEvent e) -> {
            Element qs=webEngine.getDocument().getElementById("main-container");
            NodeList nls=qs.getChildNodes();
            org.w3c.dom.Node n2=nls.item(1);
            NodeList nls2=n2.getChildNodes();
            org.w3c.dom.Node nn2=nls2.item(1);
            NodeList nns2=nn2.getChildNodes();
            org.w3c.dom.Node nnn6=nns2.item(5);
            NodeList nnns6=nnn6.getChildNodes();
            org.w3c.dom.Node nnnn3=nnns6.item(3);
            NodeList nnnns3=nnnn3.getChildNodes();
            org.w3c.dom.Node li3=nnnns3.item(3);
            NodeList lis3=li3.getChildNodes();
            org.w3c.dom.Node lidiv3=lis3.item(3);
            String periodNumber_c=lidiv3.getTextContent().substring(0,3);
            System.out.println("periodNumber_c:"+periodNumber_c);
            org.w3c.dom.Node lidiv7=lis3.item(7);
            String awardNumbers_c=lidiv7.getTextContent().charAt(0)+","+lidiv7.getTextContent().charAt(1)+","+lidiv7.getTextContent().charAt(2)+","+lidiv7.getTextContent().charAt(3)+","+lidiv7.getTextContent().charAt(4);
            System.out.println("awardNumbers_c:"+awardNumbers_c);

            org.w3c.dom.Node li5=nnnns3.item(5);
            NodeList lis5=li5.getChildNodes();

            Element qs1=webEngine.getDocument().getElementById("period");

            String periodNumber_n=qs1.getTextContent().substring(1,4);
            System.out.println("periodNumber_n:"+periodNumber_n);

            org.w3c.dom.Node lidiv13=lis5.item(3);
//            System.out.println(lidiv13.getNodeName());
            int fend=Integer.valueOf(lidiv13.getTextContent());
            System.out.println("分："+fend);

            org.w3c.dom.Node lidiv17=lis5.item(7);
            int miao=Integer.valueOf(lidiv17.getTextContent());
//            System.out.println(lidiv17.getNodeName());
            System.out.println("秒："+miao);

            long awardTimeInterval=fend*60*1000+miao*1000;
            int periodNumber_c_int=Integer.parseInt(periodNumber_c);
            int periodNumber_n_int=Integer.parseInt(periodNumber_n);
            if(awardNumbers_c.matches("\\d,\\d,\\d,\\d,\\d")){
                awardNumbers_c_st=awardNumbers_c;
            }
            if(awardNumbers_c.equals("等,待,开,奖,.")||(periodNumber_n_int-periodNumber_c_int==2)){
                awardTimeInterval=-10;
                int i = Integer.parseInt(periodNumber_n);
                i=i-1;
                NumberFormat nf = NumberFormat.getInstance();
                //设置是否使用分组
                nf.setGroupingUsed(false);
                nf.setMaximumIntegerDigits(3);
                nf.setMinimumIntegerDigits(3);
                periodNumber_n=nf.format(i);
            }
            System.out.println("awardTimeInterval:"+awardTimeInterval);

            Date date=new Date();
            String periodDate_c=new SimpleDateFormat("yyyyMMdd").format(date);
            String awardTime_c=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
            JSONObject cur=new JSONObject();
            cur.put("periodDate",periodDate_c);
            cur.put("periodNumber",periodNumber_c);
            cur.put("awardNumbers",awardNumbers_c_st);
            String periodDate_n=periodDate_c;
            if(Integer.parseInt(periodNumber_c)==120){
                Calendar calendar   =   new GregorianCalendar();
                calendar.setTime(date);
                calendar.add(calendar.DATE,1);
                date=calendar.getTime();
                periodDate_n=new SimpleDateFormat("yyyyMMdd").format(date);
            }
            JSONObject nxt=new JSONObject();
            nxt.put("periodDate",periodDate_n);
            nxt.put("periodNumber",periodNumber_n);
            nxt.put("awardTimeInterval",awardTimeInterval);

            JSONObject sscdata=new JSONObject();
            sscdata.put("next",nxt);
            sscdata.put("current",cur);
            Jedis jedis = new Jedis("120.77.215.124", 6379);
            jedis.auth("123456");
            jedis.set("sscdata",sscdata.toJSONString());
            System.out.println(jedis.get("sscdata"));
            File f = new File("D:\\abc.txt");
            if (f.exists()) {
                System.out.print("文件存在");
            } else {
                System.out.print("文件不存在");
                try {
                    f.createNewFile();// 不存在则创建
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            BufferedWriter output = null;
            try {
                output = new BufferedWriter(new FileWriter(f));

                output.write(jedis.get("sscdata"));
                output.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        });
        toolBar.getChildren().add(bt);      
        webEngine.load("http://123.60.198.34/Cqssc/index.html");
        getChildren().add(toolBar);
        getChildren().add(browser);
    }

    // JavaScript interface object
    public class JavaApp {

        public void exit() {
            Platform.exit();
        }
    }

    private Node createSpacer() {
        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        return spacer;
    }

    @Override
    protected void layoutChildren() {
        double w = getWidth();
        double h = getHeight();
        double tbHeight = toolBar.prefHeight(w);
        layoutInArea(browser,0,0,w,h-tbHeight,0,HPos.CENTER,VPos.CENTER);
        layoutInArea(toolBar,0,h-tbHeight,w,tbHeight,0,HPos.CENTER,VPos.CENTER);
    }

    @Override
    protected double computePrefWidth(double height) {
        return 900;
    }

    @Override
    protected double computePrefHeight(double width) {
        return 600;
    }
}
